Introduction to R shiny package

Hakan Turgay & Bertan Taylan

Chapter 1

Motivation, Basic R-Shiny Package and Example

Motivation

  • Scientists mostly use R to process their analyzes

  • Presenting/Sharing their findings are usually done in static format

  • Problem : They cannot present additional questions directly

  • Simple idea: Immigrating ratios from a specific region of Turkey due to years and also forecasts about these ratios (e.g. Journalist)

What is Shiny package?

  • Shiny is an R package that makes it easy to build interactive web applications (apps) straight from R. This lesson will get you started building Shiny apps right away.
  • Build useful web applications with only a few lines of code—no JavaScript required.
  • Shiny applications are automatically “live” in the same way that spreadsheets are live. Outputs change instantly as users modify inputs, without requiring a reload of the browser.
  • Shiny user interfaces can be built entirely using R, or can be written directly in HTML, CSS, and JavaScript for more flexibility.
  • Pre-built output widgets for displaying plots, tables, and printed output of R objects.

Basically


R Shiny = R + interactivity + web made easy

kmeans

Install Shiny Package

If you still haven’t installed the Shiny package, open an R session, connect to the internet, and run

First App in Shiny

Code Output

firstapp

More Example

Chapter 2

Structure of a Shiny App

Main Structure

  • ui: Nested R functions that assemble an HTML user interface for the app

  • server : A function with instructions on how to build and rebuild the R objects displayed in the UI

  • shinyApp : Combines ui and server into a functioning app

  • Save the template as app.R

  • a call to the shinyApp function

Alternative Approach

kmeans

Say Hello with Shiny

Code Output

working code:

helloshiny

Chapter 3

Inputs and Outputs

Overview

Build your app around inputs and outputs

kmeans

Input Syntax

kmeans

Input

Code Output

input

Entegration Input and Output

Use 3 rules to write the server function

1 - Save objects to display to output$

2 - Build objects to display with render*()

[ING] UI tarafinda bulunan ‘hist’ (histogram) objesi server tarafindaki ‘RenderPlot’ methodu ile uretilecek . Not: hist ve RenderPlot vurgulanacak ..

Render Method

First Rule

3 - Access input values with input$

First Rule

…and more “Render Method” example

First Rule

Result (Reactivity)

Reactivity automatically occurs whenever you use an input value to render an output object

Code Output

working code:

res

Chapter 4

Using Checkbox in Shiny

Reviewing an Example

kmeans

Starting to Code Analysis

kmeans

Change code

Result

working code:

check

Chapter 5

Improving the Design

Design Part

  • Assemble UI with HTML/CSS/… widgets

  • Adjustment of the layout scheme

Chapter 6

Implementations in Shiny: JavaScript, HTML and CSS

Using HTML, CSS and Javascript in R Code


In R, HTML elements can be defined by tags keyword.

…and more Html Tag

html tag

External file import (html, css , js ext)

Method of importing depends on type of the file;

To include a CSS file


use includeCSS() or
1. Place the file in the www subdirectory
2. Link to it with:

To include JavaScript


use includeScript() or
1. Place the file in the www subdirectory
2. Link to it with:

To include HTML file

Layout structure of the UI

Combine multiple elements into a ”single element” that has its own properties with a panel function

Chapter 7

Introduction to HTMLWidgets Package

Overview

  • This package builds a framework for creating R bindings to JavaScript libraries. HTMLWidgets can be
    • Used at the R console for data analysis
    • Embedded within R Markdown documents
    • Incorporated into Shiny web applications

Creating a Widget

  • Three elements forms the widget:

    • Dependencies : JavaScript and CSS assets will be used by the widget
    • R binding : This is the function where R stuff happens
    • JavaScript binding : JavaScript code that connects everything and passes the data and the choices collected from the R binding to the JavaScript library which is (are) using in widget

Some most-used HTMLWidget Examples

kmeans


leaflet

kmeans


dygraph

Creating a widget step-by-step

Requirements

  • We need to create a new R package that relies on the htmlwidgets package.

Scaffolding

To create a new widget you can call the scaffoldWidget function to generate the basic structure for your widget. This function will:

  • Create the .R, .js, and .yaml files required for your widget
  • Tip: If provided, take a Bower package (which is a package manager for web) name and automatically download the JavaScript library (and its dependencies) and add the required entries to the .yaml file. This method is highly preferrable because it guarantees that you get started with the right file structure.

MyWidget

We want to create a widget named ‘mywidget’ in a new package of the same name:

  • This creates a simple widget that takes a single text argument and displays that text within the widgets HTML element. You can try it like this:
  • This is the most minimal widget possible and doesn’t yet include a JavaScript library.

Chapter 8

Case Studies: Predicted Deaths from Lung Disease

Using DyGraph Package in Shiny

Ui part of the code:

Server part of the code

Result

check

Chapter 9

Case Studies: Migration

Retrieving Data

Using Leaflet

  • For creating a map we used Leaflet library.
  • Leaflet is a open source JavaScript library for creating interactive maps.
  • It can be implemented and used in Shiny easily.
  • Leaflet gives the opportunity of adding informational popups to particular part of maps.
  • leaflet

Result

result